package Factory;

import java.net.InetAddress;
import java.util.ArrayList;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: nelson
 */
public class ConnectionState {
     private InetAddress ipOri;
    private InetAddress ipDes;

    private int threshold;
    private int lastAck;
    private int lastSent;
    private int numSeq;
    private int numAck;
    private int winSize;
    private int URG,ACK,PSH,RST,SYN,FIN;

    private String state;

    private ArrayList<Byte> buffer;
    private ArrayList<Boolean> controlo;

    public ConnectionState()
	{
        //Este servem para construir os pacotes a enviar com base nestas variaveis
        this.ipOri = null;
        this.ipDes = null;
        this.threshold = 65535;
        this.numSeq = 0;
        this.numAck = 0;
        this.winSize = 512;
        this.URG = 0;
        this.ACK = 0;
        this.PSH = 0;
        this.RST = 0;
        this.SYN = 0;
        this.FIN = 0;

		//buffer
		this.buffer = new ArrayList<Byte>();
		this.controlo = new ArrayList<Boolean>();

		//estado
        this.state = "connect";
    }

	public Pacote2 criaPacote(InetAddress ip, byte[] data, int tamanho)
	{
		Pacote2 p = new Pacote2(ipOri,ip,0,0,numSeq,numAck,winSize,URG,ACK,PSH,RST,SYN,FIN,null,data,tamanho);
		p.setChecksum((p.checksum16(data,0,tamanho)));

		return p;
	}

	public void enviaPacote(Pacote2 p, DataOutputStream dos)
	{
		byte[] barray = p.toByteArray();
		byte[] tarray = p.intIn2bytes(barray.length);
		byte[] iparray = p.getIpDes().getAddress();

		//System.out.print("   ENV PAC t:" + tarray.length + " ip:"+iparray.length+" data:"+barray.length+"!\n");
		try{

			for(byte b : tarray)
				dos.write(b);

			for(byte b : iparray)
				dos.write(b);

			for(byte b : barray)
				dos.write(b);

		} catch (IOException ex) {
			ex.printStackTrace();
		}
	}

	public int getLastAck() {
		return lastAck;
	}

	public void setLastAck(int lastAck) {
		this.lastAck = lastAck;
	}

	public int getLastSent() {
		return lastSent;
	}

	public void setLastSent(int lastSent) {
		this.lastSent = lastSent;
	}




	public int getACK() {
		return ACK;
	}

	public void setACK(int ACK) {
		this.ACK = ACK;
	}

	public int getFIN() {
		return FIN;
	}

	public void setFIN(int FIN) {
		this.FIN = FIN;
	}

	public int getPSH() {
		return PSH;
	}

	public void setPSH(int PSH) {
		this.PSH = PSH;
	}

	public int getRST() {
		return RST;
	}

	public void setRST(int RST) {
		this.RST = RST;
	}

	public int getSYN() {
		return SYN;
	}

	public void setSYN(int SYN) {
		this.SYN = SYN;
	}

	public int getURG() {
		return URG;
	}

	public void setURG(int URG) {
		this.URG = URG;
	}

	public ArrayList<Byte> getBuffer() {
		return buffer;
	}

	public void setBuffer(ArrayList<Byte> buffer) {
		this.buffer = buffer;
	}

	public ArrayList<Boolean> getControlo() {
		return controlo;
	}

	public void setControlo(ArrayList<Boolean> controlo) {
		this.controlo = controlo;
	}

	public InetAddress getIpDes() {
		return ipDes;
	}

	public void setIpDes(InetAddress ipDes) {
		this.ipDes = ipDes;
	}

	public InetAddress getIpOri() {
		return ipOri;
	}

	public void setIpOri(InetAddress ipOri) {
		this.ipOri = ipOri;
	}

	public int getNumAck() {
		return numAck;
	}

	public void setNumAck(int numAck) {
		this.numAck = numAck;
	}

	public int getNumSeq() {
		return numSeq;
	}

	public void setNumSeq(int numSeq) {
		this.numSeq = numSeq;
	}

	public String getState() {
		return state;
	}

	public void setState(String state) {
		this.state = state;
	}

	public int getThreshold() {
		return threshold;
	}

	public void setThreshold(int threshold) {
		this.threshold = threshold;
	}

	public int getWinSize() {
		return winSize;
	}

	public void setWinSize(int winSize) {
		this.winSize = winSize;
	}    
}
